<template>
  <div class="role">
    <div class="search">
      <page-search
        :IFormConfig="IFormConfig"
        @handleResetClick="handleResetClick"
        @handleQueryClick="handleQueryClick"
      ></page-search>
    </div>
    <div class="content">
      <page-content
        ref="pageContentRef"
        :ContentTableConfig="ContentTableConfig"
        pageName="role"
        @newBtnClick="handleNewData"
        @editBtnClick="handleEditData"
      ></page-content>
    </div>
    <page-model
      ref="pageModelRef"
      pageName="role"
      :defaultInfo="defaultInfo"
      :otherInfo="otherInfo"
      :modelConfig="modelConfig"
    >
      <div class="menu-tree">
        <el-tree
          ref="elTreeRef"
          :data="menus"
          @check="handleCheckChange"
          show-checkbox
          node-key="id"
          :props="defaultProps"
        ></el-tree>
      </div>
    </page-model>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, ref, nextTick } from 'vue'
import PageContent from '@/components/page-content/index'
import PageSearch from '@/components/page-search/index'
import PageModel from '@/components/page-model/index'
import { ContentTableConfig } from './config/content.cofig'
import { IFormConfig } from './config/search.config'
import { usePageSearch } from '@/hooks/usePageSearch'
import { usePageModel } from '@/hooks/usePageModel'
import { modelConfig } from './config/model.config'
import { useStore } from '@/store'
import { mapMenuLeafKeys } from '@/utils/map-menus'
import { ElTree } from 'element-plus'
export default defineComponent({
  name: 'role',
  components: { PageContent, PageSearch, PageModel },
  setup() {
    // 处理pageModel的hook 拿到row数据回显树节点
    const elTreeRef = ref<InstanceType<typeof ElTree>>()
    const editCallback = (item: any) => {
      const leafKeys = mapMenuLeafKeys(item.menuList)
      nextTick(() => {
        elTreeRef.value.setCheckedKeys(leafKeys, false)
      })
    }
    // 获取组件实例调用方法
    const [pageContentRef, handleResetClick, handleQueryClick] = usePageSearch()
    const [pageModelRef, defaultInfo, handleNewData, handleEditData] = usePageModel(undefined, editCallback)
    const defaultProps = { children: 'children', label: 'name' }
    const store = useStore()
    const menus = computed(() => store.state.entireMenu)
    const otherInfo = ref({})
    const handleCheckChange = (data1: any, data2: any) => {
      const checkedKeys = data2.checkedKeys
      const halfCheckedKeys = data2.halfCheckedKeys
      const menuList = [...checkedKeys, ...halfCheckedKeys]
      otherInfo.value = { menuList }
    }
    return {
      ContentTableConfig,
      IFormConfig,
      modelConfig,
      pageContentRef,
      handleResetClick,
      handleQueryClick,
      pageModelRef,
      defaultInfo,
      handleNewData,
      handleEditData,
      defaultProps,
      menus,
      otherInfo,
      handleCheckChange,
      elTreeRef
    }
  }
})
</script>

<style scoped lang="less">
.search {
  background-color: #fff;
  border-radius: 5px;
  margin-bottom: 20px;
}
.content {
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
}
.menu-tree {
  margin-left: 70px;
}
</style>
